<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');
jimport('joomla.utilities.date');

class iJoomla_SurveysModelNewskip extends JModel{
		
	function getAllSurveys(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('title, id');
		$query->from('#__ijoomla_surveys_surveys');
		$query->order("title ASC");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getAllPages($survey_id = ""){
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		
		$where = " 1=1 ";
		
		if($survey_id != ""){
			$where .= " and survey_id=".$survey_id;
		}
		
		$query->clear();		
		$query->select('title, id');
		$query->from('#__ijoomla_surveys_pages');
		$query->order("title ASC");
		$query->where($where);
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getAllQuestions(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('title, id');
		$query->from('#__ijoomla_surveys_questions');
		$query->order("title ASC");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getAnswers($id){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('a.id, a.value');
		$query->from('#__ijoomla_surveys_answers as a');
		$query->where("a.question_id=".$id);
		$query->order("a.id asc");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getPagesTarget($id, $page_id){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('id, title');
		$query->from('#__ijoomla_surveys_pages');
		$query->where("survey_id=".$id." and id<>".$page_id);
		$db->setQuery($query);
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
		
	function getSkipValues(){
		$id = JRequest::getVar("id", "0");
		if($id != "0"){
			$db =& JFactory::getDBO();		
			$query = $db->getQuery(true);
			$query->clear();	
			$query->select('sk.*, GROUP_CONCAT(distinct a.value order by a.id asc) as answer_choises');
			$query->from('#__ijoomla_surveys_skip_logics as sk, #__ijoomla_surveys_answers as a');
			$query->where("sk.id =".$id." and sk.question_id=a.question_id");				
			$db->setQuery($query);		
			$db->query();
			$result = $db->loadAssocList();				
			return $result;
		}
		else{
			return "";
		}
	}	
	
	function setQuestionRequired($id){
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->clear();
		$query->update("#__ijoomla_surveys_questions");
		$query->set("required=1");
		$query->where("id=".$id);
		$db->setQuery($query);
		$db->query();
	}
	
	function save(){
		$db =& JFactory::getDBO();		
		$table =& $this->getTable();		
		$return = array();		
		$id = JRequest::getVar("id", "0");		
		
		$all_dates = JRequest::get('post',JREQUEST_ALLOWRAW);		
		if(isset($all_dates["answer_value"]) && is_array($all_dates["answer_value"])){
			$temp = $all_dates["answer_value"];
			$all_dates["answer_value"] = implode(",", $temp);
		}
		
		if($id == "0"){
			$ordering = $this->getMaxOrdering();
			$all_dates["ordering"] = $ordering;
		}
		else{
			unset($all_dates["ordering"]);
		}
		
		// Bind the form fields to the news table		
		if (!$table->bind($all_dates)){
			echo $this->_db->getErrorMsg(); exit;
			$this->setError($this->_db->getErrorMsg());
			$return["0"] = false;
		}
		// Make sure the news record is valid
		if (!$table->check()){
			echo $this->_db->getErrorMsg(); exit;		
			$this->setError($this->_db->getErrorMsg());
			$return["0"] = false;
		}		
		// Store the web link table to the database
		if (!$table->store()){
			echo $this->_db->getErrorMsg(); exit;
			$this->setError( $this->_db->getErrorMsg() );
			$return["0"] = false;
		}
		
		$this->setQuestionRequired($all_dates["question_id"]);
		
		$return["0"] = true;
		$return["1"] = $table->id;
	
		return $return;
	}
	
	function getMaxOrdering(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('max(ordering)');
		$query->from('#__ijoomla_surveys_skip_logics');
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadResult();
		return (int)($result)+1;
	}
}

?>